﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientAppViaWebService.Utility;
using ClientAppViaWebService.BaseLogicServiceRef;
using ClientAppViaWebService.DBAdministration;

namespace ClientAppViaWebService.Forms
{
    public partial class MainForm : Form
    {
        protected BaseLogicServiceRef.BaseLogicService service;

        protected TObject activeNode;
        protected List<UniViewActionMenuItem> optMenu = new List<UniViewActionMenuItem>();
        protected List<String> dbStrings = new List<string>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                service = new ClientAppViaWebService.BaseLogicServiceRef.BaseLogicService();

                TreeViewLogic.InitTreeView(service, ref uniTreeView, ref itemContextMenuStrip);
            }
            catch  (Exception ex)
            {
                MessageBox.Show("Failed to load the navigation tree. Can't connect to the remote web service. Go to the 'Setup->Run  WebServices Setup' menu tab to set the correct settings'");
            }
        }

        private void uniTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Tag is TObject)
            {
                TreeNode node = e.Node;
                TreeViewLogic.LoadChildrenByParentTobj(service, ref node, e.Node.Tag as TObject, ref itemContextMenuStrip);
            }
            return;
        }

        private void uniTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            uniTreeView.SelectedNode = e.Node;
            if (e.Node.Tag.ToString().Equals("-1"))
            {
                return;
            }

            if (e.Button == MouseButtons.Right)
            {
                uniTreeView.SelectedNode = e.Node;
                itemContextMenuStrip.Items[1].Visible = itemContextMenuStrip.Items[2].Visible = false;
                if (uniTreeView.SelectedNode.Tag is TObject && ((TObject)uniTreeView.SelectedNode.Tag).CLASS_ID != 0)
                {
                    itemContextMenuStrip.Items[1].Visible = itemContextMenuStrip.Items[2].Visible = true;
                    activeNode = uniTreeView.SelectedNode.Tag as TObject;
                }
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (uniTreeView.SelectedNode.Tag is TObject)
            {
                InsertForm form = null;
                if (uniTreeView.SelectedNode.Tag is TObject && ((TObject)uniTreeView.SelectedNode.Tag).CLASS_ID != 0)
                {
                    form = new InsertForm((TObject)uniTreeView.SelectedNode.Tag);
                }
                else
                {
                    form = new InsertForm(new TObject());
                }
                if (form.ShowDialog() == DialogResult.OK)
                {
                    TreeNode pt = uniTreeView.SelectedNode;
                    if (uniTreeView.SelectedNode.Nodes.Count == 0)
                    {
                        pt.Nodes.Add(new TreeNode());
                    }
                    if (pt.IsExpanded)
                    {
                        pt.Collapse();
                        pt.Expand();
                    }
                }
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (uniTreeView.SelectedNode.Tag is TObject)
            {
                TObject tobj = (TObject)uniTreeView.SelectedNode.Tag;
                
                TObjectEditForm form = ReflectionLogic.CreateFormByTObjectClassId(tobj.DataBaseName, tobj.CLASS_ID);

                form.SetForm(tobj,
                ClientAppViaWebService.Forms.TObjectEditForm.eMode.mdEdit,
                tobj.DataBaseName);

                if (form.ShowDialog() == DialogResult.OK)
                {
                    uniTreeView.SelectedNode.Text = form.ObjName;
                }
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (uniTreeView.SelectedNode.Tag is TObject)
            {
                if (MessageBox.Show(this, "Are you sure want to delete this node?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    TObject tobj = (TObject)uniTreeView.SelectedNode.Tag;
                    //here must be cascade deleting by service                 

                    service.DeleteTobject(tobj);
                    uniTreeView.Nodes.Remove(uniTreeView.SelectedNode);
                }
            }
        }

        private void runTableAdministratorToolStripMenuItem_Click(object sender, EventArgs e)
        {         
            ChooseDataBaseForm f = new ChooseDataBaseForm();
            if (f.ShowDialog() == DialogResult.OK)
            {
                RootSchemaTablesAdministrationForm rootForm = new RootSchemaTablesAdministrationForm(f.SelectedDataBase);

                rootForm.ShowDialog(this);               
            }
        }

        private void runWebServicesSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebServicesSettingForm f = new WebServicesSettingForm();
            if (f.ShowDialog() == DialogResult.OK)
            {
                this.Hide();
                Application.Restart();
            }
        }

        private void runRuleInsertAdministratorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RuleInsertAdministartionForm f = new RuleInsertAdministartionForm();
            f.ShowDialog(this);
        }

        private void aboutTheProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutProjectForm f = new AboutProjectForm();
            f.ShowDialog(this);
        }

        private void runProcessesAdministratorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProcessesAdministartionForm f = new ProcessesAdministartionForm();
            f.ShowDialog(this);
        }

        private void uniTreeView_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
        {
        }
    }
}
